package org.example.supplychain.service;

import org.example.supplychain.domain.Order;
import org.example.supplychain.domain.TotalMaterial;
import org.example.supplychain.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Stream;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private TotalMaterialService totalMaterialService;

    /**
     * 添加订单
     * 传过来一个订单，插入到数据库中，id索引自增
     */
    public void addOrder(Order order) {
        orderMapper.addOrder(order);
    }


    /**
     * 删除订单
     *
     * @param orderId 订单id
     */
    public void deleteOrderByOrderId(long orderId) {
        orderMapper.deleteOrderByOrderId(orderId);
    }

    /**
     * 更新订单
     * 传过来一个订单实例，更新数据库中的订单信息
     *
     * @param order 订单
     */
    public void updateOrder(Order order) {
        orderMapper.updateOrderByOrderId(order);
    }


    /**
     * 根据订单id获取订单
     *
     * @param orderId 订单id
     * @return 订单
     */
    public Order getOrderByOrderId(long orderId) {
        return orderMapper.getOrderByOrderId(orderId);
    }

    /**
     * 获取所有订单
     *
     * @return 所有订单实例
     */
    public List<Order> getAllOrders() {
        return orderMapper.getAllOrders();
    }

    /**
     * 根据条件获取订单
     *
     * @param materialName  原料名
     * @param supplierName  供应商名
     * @param inventoryName 仓库名
     * @param status        订单状态
     * @param startDate     订单开始时间
     * @param endDate       订单结束时间
     * @return 检索后的订单列表
     */
    public List<Order> getOrdersByIndex(String materialName, String supplierName, String inventoryName, String status, Date startDate, Date endDate) {
        List<Order> orderList = new ArrayList<Order>();
        orderList = orderMapper.getAllOrders();
        if (materialName != null) {
            for (Order order : orderList) {
                if (!order.getMaterialName().equals(materialName)) {
                    orderList.remove(order);
                }
            }
        }
        if (supplierName != null) {
            for (Order order : orderList) {
                if (!order.getSupplierName().equals(supplierName)) {
                    orderList.remove(order);
                }
            }
        }
        if (inventoryName != null) {
            for (Order order : orderList) {
                if (!order.getInventoryName().equals(inventoryName)) {
                    orderList.remove(order);
                }
            }
        }
        if (status != null) {
            for (Order order : orderList) {
                if (!order.getStatus().equals(status)) {
                    orderList.remove(order);
                }
            }
        }
        if (startDate != null) {
            for (Order order : orderList) {
                if (order.getStartTime().before(startDate)) {
                    orderList.remove(order);
                }
            }
        }
        if (endDate != null) {
            for (Order order : orderList) {
                if (order.getEndTime().after(endDate)) {
                    orderList.remove(order);
                }
            }
        }
        return orderList;
    }

    /**
     * 获取即将到期的订单
     *
     * @return 即将到期的订单列表
     */
    public List<Order> getOrdersNearDeadline() {
        List<Order> orders = orderMapper.getOrdersNearDeadline();
        Stream<Order> stream = orders.stream();
        stream = stream.filter(order -> order.getStatus().equals("进行中"));
        return stream.toList();
    }

    /**
     * 获取库存预警的物料
     *
     * @return 预警物料列表
     */
    public List<TotalMaterial> getWarningMaterial() {
        return orderMapper.getWarningMaterial();
    }

    /**
     * 供应商管理,配有索引功能
     *
     * @return 列表
     */
    public List<Map<String, Object>> supplierManagement() {
        return orderMapper.supplierManagement();
    }

    /**
     * 获取负责订单
     *
     * @param buyerName 采购员名
     * @return 采购员负责的所有订单实例
     */
    public List<Order> getOrderByBuyerName(String buyerName) {
        List<Order> orders = getAllOrders();
        Iterator<Order> o = orders.iterator();
        while (o.hasNext()) {
            Order order = o.next();
            if (!Objects.equals(order.getBuyerName(), buyerName)) {
                o.remove();
            }
        }
        return orders;
    }

    /**
     * 采购员订单历史追踪
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 采购员负责的在起止时间内的所有订单实例
     */
    public List<Order> getOrderByOrderTime(String buyerName, Date startTime, Date endTime) {
        List<Order> orders = getOrderByBuyerName(buyerName);
        Iterator<Order> o = orders.iterator();
        while (o.hasNext()) {
            Order order = o.next();
            if (order.getStartTime().before(startTime) || order.getEndTime().after(endTime)) {
                o.remove();
            }
        }
        return orders;
    }

    /**
     * 采购员正在进行订单追踪
     *
     * @param buyerName 采购员名
     * @return 采购员负责的所有正在进行的订单实例
     */
    public List<Order> getLivingOrder(String buyerName) {
        List<Order> orders = getOrderByBuyerName(buyerName);
        Iterator<Order> o = orders.iterator();
        while (o.hasNext()) {
            Order order = o.next();
            if (Objects.equals(order.getStatus(), "已完成")) {
                o.remove();
            }
        }
        return orders;
    }

    /**
     * 采购员结束订单追踪
     *
     * @param buyerName 采购员名
     * @return 采购员负责的所有已结束的订单实例
     */
    public List<Order> getPastOrder(String buyerName) {
        List<Order> orders = getOrderByBuyerName(buyerName);
        Iterator<Order> o = orders.iterator();
        while (o.hasNext()) {
            Order order = o.next();
            if (Objects.equals(order.getStatus(), "进行中")) {
                o.remove();
            }
        }
        return orders;
    }

    public List<TotalMaterial> getLackingMaterial() {
        List<TotalMaterial> totalMaterials = totalMaterialService.getAllTotalMaterial();
        Stream<TotalMaterial> tmp = totalMaterials.stream();
        tmp = tmp.filter(totalMaterial -> totalMaterial.getMaterialTotalNumber() < totalMaterial.getMinimumStock());
        return tmp.toList();
    }

    /**
     * 调用时刷新原料总量（根据订单列表）
     * 务必读取全部订单列表的已完成订单重新计算总量
     */
    public void updateMaterialTotalNumber() {
        ;//todo
        totalMaterialService.updateMaterialTotalNumber();
    }
}
